package com.cpecc.dao.wuzi;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFeiSPR;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class BaoFeiSPRDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(BaoFeiSPR baoFeiSPR){
		Session session=sessionFactory.getCurrentSession();
		session.save(baoFeiSPR);
		session.flush();
	}
	
	public BaoFeiSPR get(String id){
		return (BaoFeiSPR) sessionFactory.getCurrentSession().get(BaoFeiSPR.class, id);
	}
	
	public boolean delete(String id){
		String hql="delete BaoFeiSPR t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}

	public List<BaoFeiSPR> getBaoFeiList(User user, Integer huangJieId) {
		String hql="from BaoFeiSPR u where u.no = 1 and u.user.id=? and u.baoFei.huanJie.id=? order by u.baoFei.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		query.setInteger(1, huangJieId);
		return query.list();
	}
	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getBfxgbmspList(User user){
		String hql="from BaoFeiSPR u where u.no = 1 and u.user.id=? and u.baoFei.huanJie.id=717 order by u.baoFei.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getMySPList(User user){
		String hql="from BaoFeiSPR u where u.no = 1 and u.user.id=? and u.baoFei.huanJie.id=707 order by u.baoFei.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getLZSPR(String baoFeiId){
		String hql="select u.user from BaoFeiSPR u where u.no = 1 and u.baoFei.id=? and u.baoFei.huanJie.id=705";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getMyBMSPList(User user){
		String hql="from BaoFeiSPR u where u.no = 1 and u.user.id=? and u.baoFei.huanJie.id in (702) order by u.baoFei.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}

	public List<User> getSPRList(String baoFeiId){
		String hql="select u.user from BaoFeiSPR u where u.no = 1 and u.baoFei.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	public List<User> getBuMenSPR(String baoFeiId){
		String hql="select u.user from BaoFeiSPR u where u.no = 1 and u.baoFei.id=? and u.baoFei.huanJie.id=702";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getBuMenSRRList(String baoFeiId){
		String hql="from BaoFeiSPR u where u.no = 1 and u.baoFei.id=? and u.baoFei.huanJie.id=702";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}

	/**
	 * 获取所有审批人
	 * @param baoFeiId	物资报废ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getAllSPR(String baoFeiId){
		String hql="from BaoFeiSPR u where u.no = 1 and u.baoFei.id=? group by u.user";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();		
	}

	@SuppressWarnings("unchecked")
	public List<BaoFeiSPR> getAllByBaoFeiId(String baoFeiId){
		String hql="from BaoFeiSPR u where u.no = 1 and u.baoFei.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}
	
	public int getBySPSX(String userId,String baoFeiId){
		String hql="from BaoFeiSPR t where t.no = 1 and t.user.id=? and t.baoFei.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, userId);
		query.setString(1, baoFeiId);
		return query.list().size();
	}
	
	public boolean deleteSPR(String baoFeiId,String userId){
		String hql="delete BaoFeiSPR t where t.no = 1 and t.baoFei.id=? and t.user.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		query.setString(1, userId);
		return query.executeUpdate()>0;
	}
	
	public boolean deleteAllSPR(String baoFeiId){
		String hql="delete from BaoFeiSPR t where t.no = 1 and t.baoFei.id = ?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.executeUpdate()>0;
	}

	/**
	 * 设置审批人的状态为待审批
	 * @param baoFeiId	主键ID
	 * @return
	 */
	public boolean updateSPRNO(String baoFeiId) {
		String hql = "update BaoFeiSPR t set t.no = 1 where t.baoFei.id = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.executeUpdate() > 0;
	}
	
}
